/**
 * Copyright (C), 2015-2018, 成都启迪信息技术有限公司
 * FileName: SmsService
 * Author:   sjkyll
 * Date:     2018-08-09 13:39
 * Description:
 * <author>          <time>          <version>          <desc>
 * 作者姓名         修改时间           版本号            描述
 */
package com.cdqidi.smsservice.service.sms;

import com.cdqidi.smsservice.domain.WlReport;
import com.cdqidi.smsservice.dto.SmsReport;
import com.cdqidi.smsservice.repository.KernelReceiveLogsRepository;
import com.cdqidi.smsservice.repository.WlReportRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 〈一句话功能简述〉<br>
 *
 * @author sjkyll
 * @create 2018-08-09 13:39l
 * @since 1.0.0
 */
@Service
@Slf4j
public class SmsService {
    private final WlReportRepository wlReportRepository;
    private final KernelReceiveLogsRepository kernelReceiveLogsRepository;

    private ConcurrentHashMap<String,Long> ids = new ConcurrentHashMap<>();
    private final static Integer locked = 1;

    @Autowired
    public SmsService(WlReportRepository wlReportRepository, KernelReceiveLogsRepository kernelReceiveLogsRepository) {
        this.wlReportRepository = wlReportRepository;
        this.kernelReceiveLogsRepository = kernelReceiveLogsRepository;
    }
    public void saveReportLogs(SmsReport report) {
        if (null == report.getMsgID() || "".equals(report.getMsgID())) {
            return;
        }
        if (null == report.getReportResultInfo() || "".equals(report.getReportResultInfo())) {
            return;
        }
        if (null == report.getMobilePhone() || "".equals(report.getMobilePhone())) {
            return;
        }
        synchronized (locked) {
            if(!ids.containsKey(report.getMsgID())){
                ids.put(report.getMsgID(),System.currentTimeMillis());
                this.saveAsyncReportLogs(report);
                report.freeData();
            }
        }
    }

    @Async
    @Transactional
    public void saveAsyncReportLogs(SmsReport report) {
        String reportResultInfo = report.getReportResultInfo();
        if (report.getReportState()) {
            reportResultInfo = "DELIVRD";
        }
        final WlReport wReport = new WlReport();
        wReport.setMsgid(report.getMsgID());
        wReport.setPhone(report.getMobilePhone());
        wReport.setClientMsgId(report.getClientMsgId());
        wReport.setReportResultInfo(reportResultInfo);
        wReport.setFlag(0);
        wReport.setTjsj(new Timestamp(System.currentTimeMillis()));
        wlReportRepository.save(wReport);
    }
    @Scheduled(cron = "0 0 * * * ?")
    public void freeSet() {
        log.info("清理set缓存数据，大小： "+ids.size());
        synchronized (locked) {
            for (String id : ids.keySet()) {
                if(System.currentTimeMillis()-(ids.get(id))>(1*60*60*1000));{
                    ids.remove(id);
                }
            }
        }
    }
}